﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using socialnetworkApp.Core;
using socialnetworkApp.Domain;
using socialnetworkApp.Domain.Dto.Input;
using socialnetworkApp.Domain.Dto.Output;
using socialnetworkApp.Service.UsersService;

namespace socialnetworkApp.Controllers
{
    [Route("api/[Controller]")]
    [ApiController]
    public class UsersController : ControllerBase
    {
          private IUserService _userService;
        public UsersController(IUserService userService) { 
        
        _userService = userService;
        }
        [HttpPost("editNickname")]
        public Task<ResponseResult<string>> UpdateNickname([FromBody] UsersUpdateInput nicknameUpdate) {

        return   _userService.UpdateNickname(nicknameUpdate);
        }

        [HttpPost("editEmail")]
        public Task<ResponseResult<string>> UpdateEmail([FromBody] UsersUpdateInput  usersUpdateInput  )
        {

            return _userService.UpdateEmail(usersUpdateInput);
        }

        [HttpPost("editSex")]
        public Task<ResponseResult<string>> UpdateSex([FromBody] UsersUpdateInput usersUpdateInput)
        {

            return _userService.UpdateSex(usersUpdateInput);
        }

        [HttpPost("editSignature")]
        public Task<ResponseResult<string>> UpdateSignature([FromBody] UsersUpdateInput usersUpdateInput)
        {

            return _userService.UpdateSignature(usersUpdateInput);
        }
        [HttpPost("userLogin")]
        public async Task<ResponseResult<LoginInfoOutput>> UserLogin(UserLoginInput userLoginInput)     { 
        
        
            return await _userService.UserLogin(userLoginInput);
        }

        [HttpPost("userLoginOut")]
        [Authorize]
        public async Task<ResponseResult<string>> UserLoginOut()
        {
            Task<ResponseResult<string>> task = Task.Run(() => {
                Thread.Sleep(1000);
                return ResponseResult<string>.Result(ResultStatus.Success, null, "成功");
            });

            ResponseResult<string> result = await task;
            return result;
        }
        [HttpPost("EditUserRoutesAndButtonsAndRoles")]
        [Authorize]
        public async Task<ResponseResult<string>> UpdateUserRoutesAndButtonsAndRoles(UsersUpdateInput  usersUpdateInput) {

          var uid =  User.GetUserId();
            usersUpdateInput.Id=uid;
            return await _userService.UpdateUserRoutesAndButtonsAndRoles(usersUpdateInput);
            

          
        }

        /// <summary>
        /// 向服务器发起请求进行邮箱验证
        /// </summary>
        /// <param name="userLoginInput"></param>
        /// <returns></returns>

        [HttpPost("sendValidateCode")]
        public ResponseResult<string> UserSendValidateCode(UserLoginInput userLoginInput) { 
        
          return  _userService.UserSendValidateCode(userLoginInput);
        }

        [HttpPost("userValidateCodeLogin")]
        public async Task<ResponseResult<LoginInfoOutput>> UserValidateCodeLogin(UserLoginInput userLoginInput)
        {
            return await  _userService.UserValidateCodeLogin(userLoginInput);
        }
        [HttpGet("getUserInfo")]
        [Authorize]
        public async Task<ResponseResult<UserInfoOutput>> GetUserInfo() {
            var uid = User.GetUserId();
            var meail=  User.GetUserEmail();
            GetUserIfoInput Id = new GetUserIfoInput() { Id=uid};
            return await _userService.GetUserInfo(Id);
        }

        [HttpGet("getFriendsList")]
        [Authorize]
        public async Task<ResponseResult<List<UserFriendsListOutput>>> GetFriendsList()
        {
            GetUserIfoInput  getUserIfoInput = new GetUserIfoInput {Id=User.GetUserId() };

            return await _userService.GetFriendsList(getUserIfoInput);
        }


        }
    }
